package com.gaoding.shared.dict.controller;

import com.gaoding.framework.web.dto.PageInfo;
import com.gaoding.framework.web.util.WebUtils;
import com.gaoding.grpc.client.annotation.GdGrpcClient;
import com.gaoding.grpc.common.protobuf.Common;
import com.gaoding.grpc.common.utils.GdBeanUtils;
import com.gaoding.shared.dict.dto.request.CreateDictServiceDTO;
import com.gaoding.shared.dict.dto.request.QueryDictServiceDTO;
import com.gaoding.shared.dict.dto.request.UpdateDictServiceDTO;
import com.gaoding.shared.dict.dto.response.DictServiceListResponseDTO;
import com.gaoding.shared.dict.grpc.CreateDictServiceListRequest;
import com.gaoding.shared.dict.grpc.DictServiceListServiceGrpc;
import com.gaoding.shared.dict.grpc.QueryDictServiceListRequest;
import com.gaoding.shared.dict.grpc.UpdateDictServiceListRequest;
import com.google.protobuf.Int64Value;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mobai
 * @since 2021/4/7 19:03
 */
@RestController
@RequestMapping("/services")
@Api(tags = "服务信息接口")
@CrossOrigin
public class DictServiceListController extends BaseController<DictServiceListResponseDTO> {

    @GdGrpcClient
    private DictServiceListServiceGrpc.DictServiceListServiceBlockingStub client;

    /**
     * @param query
     * @return java.util.List<com.gaoding.shared.dict.dto.response.DictLanguageRespDTO>
     * @since 2021/2/19 11:44 上午
     */

    @GetMapping("list")
    @ApiOperation("分页查询")
    public List<DictServiceListResponseDTO> list(QueryDictServiceDTO query) {
        int pageNum = Integer.max(query.getPageNum() - 1, 0);
        Common.Page page = Common.Page.newBuilder().setPageSize(query.getPageSize()).setPageNum(pageNum).buildPartial();
        QueryDictServiceListRequest queryParam = QueryDictServiceListRequest.newBuilder().setServiceCode(query.getServiceCode() != null ? query.getServiceCode() : "").setPage(page).build();
        if (query.getId() != null) {
            queryParam.toBuilder().setId(query.getId());
        }
        List<DictServiceListResponseDTO> result = client.list(queryParam)
                .getDataList().stream().map(data -> DictServiceListResponseDTO.builder().serviceCode(data.getServiceCode()).serviceName(data.getServiceName()).id(data.getId()).build()).collect(Collectors.toList());
        WebUtils.buildPageInfo(PageInfo.builder().num(query.getPageNum()).size(query.getPageSize()).build());
        return result;
    }

    @PostMapping("")
    @ApiOperation("新增单个服务")
    public Long create(@Valid @RequestBody CreateDictServiceDTO createDictServiceDTO) {
        CreateDictServiceListRequest.Builder builder = CreateDictServiceListRequest.newBuilder();
        GdBeanUtils.copyProperties(createDictServiceDTO, builder);
        Int64Value int64Value = client.create(builder.build());
        return int64Value.getValue();

    }

    @PutMapping("/{id}")
    @ApiOperation("更新单个服务")
    public void update(@PathVariable("id") Long id, @RequestBody @Valid UpdateDictServiceDTO updateDictServiceDTO) {
        updateDictServiceDTO.setId(id);
        UpdateDictServiceListRequest.Builder builder = UpdateDictServiceListRequest.newBuilder();
        GdBeanUtils.copyProperties(updateDictServiceDTO, builder);
        client.update(builder.build());

    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个服务")
    public void delete(@PathVariable("id") Long id) {
        client.delete(Int64Value.of(id));
    }


    @Override
    protected String[] args(DictServiceListResponseDTO dictService) {
        return new String[]{dictService.getServiceCode(),dictService.getServiceCode(), dictService.getServiceName()};
    }
}
